package com.lgx.right.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.web.Pagination;
import com.lgx.right.dao.IRoleDao;
import com.lgx.right.entity.Role;

@Component("roleDao")
public class RoleDao extends GeneralDao implements IRoleDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}
	
	@Transactional
	public Role createRole(Role role) throws DataException {
		this.getHibernateTemplate().save(role);
		log.info("[创建角色]:"+role.getId());
		return role;
	}

	@Transactional
	public Role updateRole(Role role) throws DataException {
		this.getHibernateTemplate().update(role);
		log.info("[更新角色]:"+role.getId());
		return role;
	}

	@Transactional
	public void deleteRole(Role role) throws DataException {
		log.info("[删除角色]:"+role.getId());
		this.getHibernateTemplate().delete(role);
	}

	public void deleteRole(long id) throws DataException {
		this.deleteRole(this.findRoleById(id));
		log.info("[删除角色]:"+id);
	}

	public Role findRoleById(long id) {
		return this.getHibernateTemplate().get(Role.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findRoleList(String sql) {
		return this.getHibernateTemplate().find(sql);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findRoleList(String sql, Object[] params, int pageSize,int pageNum) {
		return this.getList(sql, params, pageSize, pageNum);
	}

	public Pagination findRolePagination(String sql, Object[] params,int pageSize, int pageNum) {
		return this.getListPagination(sql, params, pageSize, pageNum);
	}

	public Pagination findRolePagination(Object[] params, int pageSize,
			int pageNum) {
		String sql = "from Role r ";
		
		if(params.length > 0 && params[0] != null)
		{
			sql += " where r.name like '%"+params[0]+"%'";
		}
		
		sql += " order by r.id asc";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	public void deleteRoles(String[] ids) throws DataException {
		for(String id : ids)
		{
			if(!"1".equals(id))
			this.deleteRole(Long.parseLong(id));
		}
	}

	/**
	 * 根据userId查找它的角色
	 * 如果userId对应的没有，就查找出全部角色
	 * distinct:返回唯一不同的值
	 * 操作的表：ts_role，ts_user_role
	 * case:简单搜索函数
	 * String sql1 =" select distinct pe.id as id,pe.name as text,pe.layerOrder as layerOrder,case when rp.role_id is not null then 'true' else null end as checked  "+
				" from ts_permission pe "+
				" left join ts_role_permission rp on pe.id = rp.permission_id and rp.role_id = "+ userId+
				" order by pe.layerOrder asc ";
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<TreeModel> findRoleUserTreeList(long userId) {
	
		String sql ="select distinct r.id,r.name as text,case when ur.user_id is not null then 'true' else null end as checked "+
					" from ts_role r "+
					" left join ts_user_role ur on r.id = ur.role_id and ur.user_id = "+ userId+
					" order by r.createtime ";
	
		return (List<TreeModel>) this.baseDao.queryJdbcForList(sql, TreeModel.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findRoleListByIds(String ids) {
		String sql = "from Role r where r.id in ("+ids+") order by r.createTime";
		return this.getHibernateTemplate().find(sql);
	}

}
